package com.taokatao.cardmee;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.taokatao.cardmee.business.MainActivity;
import com.taokatao.cardmee.R;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends BaseActivity implements View.OnClickListener {
    private int[] beginnerIds = new int[]{R.mipmap.splash_001, R.mipmap.splash_002, R.mipmap.splash_003, R.mipmap.splash_004, R.mipmap.splash_005,};
    private int[] introIds = new int[]{R.mipmap.splash_001, R.mipmap.splash_002, R.mipmap.splash_003, R.mipmap.splash_004, R.mipmap.splash_005_2,};
    private int[] coverIds;
    private List<View> splashList = new ArrayList<>();
    private List<ImageView> dotList = new ArrayList<>();
    private ViewPager viewPager;
    private ViewGroup layStepLight, layStepNormal;
    private View btnEnter;

    private ImageView iBack;

    private boolean dot = false;

    private boolean isJustFinish = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isAppDefaultTransitions = false;
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_activity_splash);

        isJustFinish = getIntent().getBooleanExtra("extra_is_just_finish", false);

        iBack = (ImageView) findViewById(R.id.i_back);
        iBack.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            iBack.setBackgroundResource(R.drawable.ripple_back_white);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final int statusBarHeight = Utils.calcStatusBarHeight(this);
            iBack.setPadding(iBack.getPaddingLeft(), iBack.getPaddingTop() + statusBarHeight, iBack.getPaddingRight(), iBack.getPaddingBottom());
        }
        iBack.setVisibility(isJustFinish ? View.VISIBLE : View.GONE);

        btnEnter = findViewById(R.id.btn_enter);
        btnEnter.setVisibility(View.GONE);
        btnEnter.setOnClickListener(this);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        layStepLight = (ViewGroup) findViewById(R.id.lay_step_light);
        layStepNormal = (ViewGroup) findViewById(R.id.lay_step_normal);

        LinearLayout.LayoutParams lpDot1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lpDot1.setMargins(0, 0, App.i().d1() * 8, 0);
        LinearLayout.LayoutParams lpDot2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        splashList.clear();

        coverIds = !isJustFinish ? beginnerIds : introIds;

        for (int i = 0; i < coverIds.length; i++) {
            int cover = coverIds[i];
            ImageView imgCover = new ImageView(this);
            imgCover.setScaleType(ImageView.ScaleType.FIT_XY);
            imgCover.setImageResource(cover);
            splashList.add(imgCover);

            if (dot) {
                ImageView imgDotNormal = new ImageView(this);
                imgDotNormal.setImageResource(R.mipmap.splash_000_step_normal);
                imgDotNormal.setLayoutParams(i < coverIds.length - 1 ? lpDot1 : lpDot2);
                layStepNormal.addView(imgDotNormal);

                ImageView imgDotLight = new ImageView(this);
                imgDotLight.setImageResource(R.mipmap.splash_000_step_light);
                imgDotLight.setLayoutParams(i < coverIds.length - 1 ? lpDot1 : lpDot2);
                imgDotLight.setAlpha(i > 0 ? 0f : 1f);
                layStepLight.addView(imgDotLight);
                dotList.add(imgDotLight);
            }
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return splashList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(splashList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(splashList.get(position));
                return splashList.get(position);
            }
        });


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int currPosition;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                currPosition = position;

                if (dot) {
                    View curr = dotList.get(currPosition);
                    curr.setAlpha(1 - positionOffset);
                    if (currPosition + 1 < dotList.size()) {
                        View next = dotList.get(currPosition + 1);
                        next.setAlpha(positionOffset);

                    }
                }

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE && currPosition >= splashList.size() - 1) {
                    btnEnter.setVisibility(View.VISIBLE);
                } else {
                    btnEnter.setVisibility(View.GONE);
                }
            }
        });

        View viewStep = findViewById(R.id.lay_step);
        viewStep.setAlpha(0f);
        viewStep.animate().alpha(1).setDuration(3000).start();
    }

    @Override
    public void onClick(View v) {
        if (v == btnEnter) {
            if (!isJustFinish) {
                Utils.spWrite(C.sp.SETTINGS_SPLASH_VERSION, Utils.getAppVersionCode());
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }else  if (v == iBack) {
            finish();
        }
    }

}
